/**
 * File Name   : QueryDao.java
 * Description : 
 */
package com.hh.common.ibatis.dao;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * Ibatis方式查询使用Dao
 * 
 * @author 冯昌硕
 * @create 2012-12-6 下午7:41:34
 */
public class QueryDaoImpl implements IQueryDao {

    private SqlMapClient sqlMapClient;
    
    /**
     * 根据SQL ID查询单条记录 适用于没有查询条件的或者无参数的SQL语句
     * 
     * @param sqlID 要查询的SQLID
     * @param clazz 查询结果Bean对应的类型
     * @return
     * @throws SQLException
     * @throws ClassCastException
     * @create 2012-12-6 下午7:58:33
     */
    @SuppressWarnings("unchecked")
    public <E> E queryForObject(String sqlID, Class<?> clazz) throws SQLException, ClassCastException {
        Object obj = sqlMapClient.queryForObject(sqlID);
        
        E rObj = null;
        
        if (clazz != null && obj != null) {
            rObj = (E) clazz.cast(obj);
        }
        
        return rObj;
    }
    
    /**
     * 根据SQLID和指定的参数查询单条记录
     * 
     * @param sqlID 要查询的SQLID
     * @param bindParams 查询参数
     * @param clazz 查询结果Bean对应的类型
     * @return
     * @throws SQLException 
     * @create 2012-12-6 下午7:51:18
     */
    @SuppressWarnings("unchecked")
    public <E> E queryForObject(String sqlID, Object bindParams, Class<?> clazz) throws SQLException, ClassCastException {
        Object obj = sqlMapClient.queryForObject(sqlID, bindParams);
        
        E rObj = null;
        
        if (clazz != null && obj != null) {
            rObj = (E) clazz.cast(obj);
        }
        
        return rObj;
    }
    
    /**
     * 根据SQLID和指定的参数查询多条记录
     * 
     * @param sqlID 要查询的SQLID
     * @param bindParams 查询参数
     * @return
     * @throws SQLException 
     * @create 2012-12-6 下午8:36:17
     */
    @SuppressWarnings("unchecked")
    public <E> List<E> queryForObjectList(String sqlID, Object bindParams) throws SQLException {
        List<E> list = sqlMapClient.queryForList(sqlID, bindParams);
        return list;
    }

    public void setSqlMapClient(SqlMapClient sqlMapClient) {
        this.sqlMapClient = sqlMapClient;
    }
    
    
}
